package org.example.ruoxing_admin_springboot3.viewmodel;
import org.example.ruoxing_admin_springboot3.dao.SystemResourcesDao;
import java.util.*;
public class ResourcesTreeBuilder {
    /**
     * @description 全量构建资源树
     * @param resourcesList 全量资源列表
     * @author 莫若省
     * @createTime 2024/5/8
     */
    public static List<SystemResourcesDao> buildTree(List<SystemResourcesDao> resourcesList) {
        Map<String, SystemResourcesDao> resourceMap = new HashMap<>();
        List<SystemResourcesDao> tree = new ArrayList<>();
        // 将所有资源节点存储在map中，键为resourcesId
        for (SystemResourcesDao resource : resourcesList) {
            resourceMap.put(resource.getResourcesId(), resource);
        }
        // 遍历资源列表，构建树结构
        for (SystemResourcesDao resource : resourcesList) {
            // 如果是根节点，则添加到树中
            if (Boolean.TRUE.equals(resource.getIsRoot())) {
                // 设置redirect并添加到树中
                setRedirectAndAddToTree(resource, resourceMap, tree);
            } else {
                // 非根节点，找到其父节点并添加到父节点的children中
                String parentNodeId = resource.getParentNodeId();
                if (parentNodeId != null && resourceMap.containsKey(parentNodeId)) {
                    SystemResourcesDao parentResource = resourceMap.get(parentNodeId);
                    // 设置redirect并添加子节点
                    setRedirectAndAddChild(parentResource, resource, resourceMap);
                }
            }
        }
        // 对树中的所有层级进行排序
        sortTree(tree);
        return tree;
    }
    private static void setRedirectAndAddToTree(SystemResourcesDao resource, Map<String, SystemResourcesDao> resourceMap, List<SystemResourcesDao> tree) {
        // 设置当前层的redirect
        resource.setRedirect(getChildPath(resource, resourceMap));
        tree.add(resource);
        // 递归添加子节点
        addChildren(resource, resourceMap);
    }
    private static void setRedirectAndAddChild(SystemResourcesDao parentResource, SystemResourcesDao childResource, Map<String, SystemResourcesDao> resourceMap) {
        if (parentResource.getChildren() == null) {
            parentResource.setChildren(new ArrayList<>());
        }
        parentResource.getChildren().add(childResource);
        // 递归设置redirect并添加子节点
        setRedirectAndAddChildRecursively(parentResource, resourceMap);
    }
    private static String getChildPath(SystemResourcesDao parentResource, Map<String, SystemResourcesDao> resourceMap) {
        if (parentResource.getChildren() != null && !parentResource.getChildren().isEmpty()) {
            return parentResource.getChildren().getFirst().getPath();
        }
        // 如果没有子节点，返回null
        return null;
    }
    private static void setRedirectAndAddChildRecursively(SystemResourcesDao parentResource, Map<String, SystemResourcesDao> resourceMap) {
        List<SystemResourcesDao> children = parentResource.getChildren();
        if (children != null) {
            for (SystemResourcesDao child : children) {
                // 为每个子节点设置redirect
                child.setRedirect(getChildPath(child, resourceMap));
                if (!resourceMap.containsKey(child.getResourcesId())) {
                    continue;
                }
                for (SystemResourcesDao resource : resourceMap.values()) {
                    if (resource.getParentNodeId() != null && resource.getParentNodeId().equals(child.getResourcesId())) {
                        children.add(resource);
                        // 递归调用以添加更深层次的子节点
                        addChildren(resource, resourceMap);
                    }
                }
            }
        }
    }
    private static void addChildren(SystemResourcesDao parentResource, Map<String, SystemResourcesDao> resourceMap) {
        List<SystemResourcesDao> children = parentResource.getChildren();
        if (children != null) {
            for (SystemResourcesDao child : children) {
                // 为每个子节点找到其子节点并添加
                if (!resourceMap.containsKey(child.getResourcesId())) {
                    continue;
                }
                for (SystemResourcesDao resource : resourceMap.values()) {
                    if (resource.getParentNodeId() != null && resource.getParentNodeId().equals(child.getResourcesId())) {
                        children.add(resource);
                        // 递归调用以添加更深层次的子节点
                        addChildren(resource, resourceMap);
                    }
                }
            }
        }
    }
    private static void sortTree(List<SystemResourcesDao> tree) {
        tree.sort(Comparator.comparingInt(SystemResourcesDao::getResourcesLevel));

        for (SystemResourcesDao resource : tree) {
            if (resource.getChildren() != null) {
                // 递归对子树进行排序
                sortTree(resource.getChildren());
            }
        }
    }
}
